package com.softserveinc.cheatsheet.services;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.security.authentication.jaas.AuthorityGranter;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.softserveinc.cheatsheet.entity.UserAccount;
import com.softserveinc.cheatsheet.entity.UserAuthority;

public class UserServiceCheck {

	private static UserService service;

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public static void main(String[] args) throws Exception {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml", UserServiceCheck.class);

		service = ctx.getBean("userService", UserService.class);

		service.createAuthority(UserAuthority.ROLE_USER);
		UserAccount user;

		for (int i = 0; i < 5; i++) {
			user = new UserAccount("User " + i);
			System.out.println(user);
			service.saveUser(user);
		}
//		UserAccount user2 = (UserAccount) service.loadUserByUsername("User 2");
//		System.out.println(user2.getId());
//		System.out.println(user2);

		List<UserAccount> allUsers = service.getAllUsers();

		System.out.println("all users:");
		for (UserAccount account : allUsers) {
			System.out.println(account);
		}

	}
}
